    //
//  GTMTesterViewController.m
//  LecturePad
//
//  Created by vo loc on 12/31/10.
//  Copyright 2010 Epsilon Mobile. All rights reserved.
//

#import "GTMTesterViewController.h"

@implementation TestCaseResult

@synthesize testName;
@synthesize startDate;
@synthesize runningTime;
@synthesize testCase;
@synthesize status;

- (id)init
{
 	self = [super init];
  if( self )
  {
   	status = kTestCaseStatusUnknown; 
  }
  
  return self;
}

- (void)runTest
{
  startDate = [NSDate date];
  
  @try 
  {
    status = kTestCaseStatusPassed;
    [testCase performTest];
  } @catch (NSException *exception) 
  {
    status = kTestCaseStatusFailed; // failed
  }
  
  runningTime = [[NSDate date] timeIntervalSinceDate:startDate];
}

- (void)reset
{
  startDate = nil;
 	runningTime = 0;
  status = kTestCaseStatusUnknown;
}
@end


@implementation GTMTesterViewController

-	(IBAction)close:(id)sender
{
 	[self dismissModalViewControllerAnimated:YES]; 
}
- (IBAction)runAllTest:(id)sender
{
  for( NSArray* suiteTests in _testCases )
  {
   	for( TestCaseResult* testCase in suiteTests )
    {
      [self performSelectorInBackground:@selector(runTest:) withObject:testCase];
    }
  }
}

- (IBAction)reset:(id)sender
{
  for( NSArray* suiteTests in _testCases )
  {
   	for( TestCaseResult* testCase in suiteTests )
    {
      [testCase reset];
    }
  }	
  
  [self refresh];
}

- (void)runTest:(id)sender
{
  NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
  
  TestCaseResult* testCase = (TestCaseResult*)sender;
  [testCase runTest];
  
  [self performSelectorOnMainThread:@selector(refresh) withObject:nil waitUntilDone:NO];
  
  [pool release];
}

- (void)refresh
{
  [tableView reloadData];
}

- (void)viewDidLoad {
  [super viewDidLoad];
  
  [tableView reloadData];
}

- (void)analyzeTestSuites:(NSArray*)testSuites
{
  [_testSuiteName release];
  _testSuiteName = [testSuites retain];
  
  [_testCases release];
  _testCases = [[NSMutableArray alloc] init];
  
 	// extract test cases from each suite
  for( NSString* suiteName in _testSuiteName )
  {
   	Class suiteClass = objc_getClass([suiteName UTF8String]);
    
    NSMutableArray * suiteCases = [[NSMutableArray alloc] init];
    [_testCases addObject:suiteCases];
    [suiteCases release];
    
    NSArray *invocations = [suiteClass testInvocations];
    if ([invocations count]) 
    {
      NSInvocation *invocation;
      GTM_FOREACH_OBJECT(invocation, invocations) 
      {
        GTMTestCase *testCase = [[suiteClass alloc] initWithInvocation:invocation];
        
        TestCaseResult *result = [[TestCaseResult alloc] init];
        [suiteCases addObject:result];
        [result release];
        
        result.testCase = testCase;
        result.testName = NSStringFromSelector([invocation selector]);
      }
    }
  }
  
  [tableView reloadData];
}

- (void)runTestAtIndexPath:(NSIndexPath*)indexPath
{
  NSArray* suiteTests = [_testCases objectAtIndex:indexPath.section];
  TestCaseResult* testCase = [suiteTests objectAtIndex:indexPath.row];
  [self performSelectorInBackground:@selector(runTest:) withObject:testCase];
}

#pragma mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
  [self runTestAtIndexPath:indexPath];
}

#pragma mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
 	return [_testCases count]; 
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
 	return [_testSuiteName objectAtIndex:section]; 
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
 	return [[_testCases objectAtIndex:section] count]; 
}
// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)

- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  static NSString* CellIdentifier = @"Cell";
  
  UITableViewCell* cell = [tv dequeueReusableCellWithIdentifier:CellIdentifier];
  if( cell == nil )
  {
   	cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
  }
  
  TestCaseResult* test = [[_testCases objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
  
  cell.textLabel.text = test.testName;
  cell.detailTextLabel.text = [NSString stringWithFormat:@"%0.3f", test.runningTime];
  
  if( test.status == 1 )
  {
   	cell.textLabel.textColor = [UIColor greenColor]; 
  }
  else if(test.status == 0) 
  {
    cell.textLabel.textColor = [UIColor redColor];
  }
  else {
    cell.textLabel.textColor = [UIColor blackColor];
  }

  
  return cell;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Overriden to allow any orientation.
    return YES;
}


- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}


- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end
